package com.dr.java.net.browser;

import java.io.*;
import java.net.Socket;

/**
 * Created by rui on 2018/4/24.
 */
public class MyBrowser {

    public static void main(String[] args) {

//        https://www.cnblogs.com/render-inside/p/7007567.html

        Socket socket = null;
        BufferedReader readFromInternet = null;
        PrintStream printer = new PrintStream(System.out,true);
        PrintStream writer=null;

        try {
            socket = new Socket("www.baidu.com", 80);
            writer=new PrintStream(socket.getOutputStream(),true);

//            必须是完整的地址，原因不太清除
            writer.println("GET http://www.baidu.com/ HTTP/1.1");
//          注意不要使用gzip，这种压缩方式需要专门的解码器
            writer.println("Accept-Encoding:");
            writer.println("Cookie: H_PS_PSSID=1435_21119_26105; BD_HOME=1; BD_UPN=143254; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; PSINO=3; BD_CK_SAM=1; H_PS_645EC=75abUNLYIjmTtWJ%2FaIMMHJdxoa4k4prxkQWmQFr0FLzzKYPSn28ejQO1jpazN89oDNUDOQ; BDSFRCVID=K-IsJeC62w_kYLrAYghKudO_R22djUrTH6aIN9gbsvPXf3ScRBmaEG0PDf8g0KubLoJuogKKXgOTHw3P; H_BDCLCKID_SF=tJuO_D8XJCK3Hn-khtnqK4AehxtX5-RLf20La4OF5l8-h433MprdKx_4bU7i-439XauHoJb_Q4oxOKQpyfIBbPDy5R3vKjJgKarGWRjN3KJmsbL9bT3v5tDThP6t2-biWabM2MbdbKOmbRO4-TFajjb3jfK; BAIDUID=11C09154C61C05904B642A5FF105DF5A:FG=1; MCITY=-9002%3A75%3A; BIDUPSID=22382B8934B6EFF7ECE097DF1FBD1142; PSTM=1523539670; BDUSS=0JheFQ0cnhaNXdIZzloNjlxSUZIaUo4fmc5MnZWQkpBcWp6ZFdzZTBpc3hnZTFaSVFBQUFBJCQAAAAAAAAAAAEAAADI68wkcmVmb3JtaW5namlhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADH0xVkx9MVZd; __cfduid=d4906999e462bbea6b51e0d7c97dd3ff81508510119");
            writer.println("Host: www.baidu.com");
            writer.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//            迟迟不结束可以将keep-alive改成close
            writer.println("Connection: close");
            writer.println("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15");
            writer.println("Accept-Language: zh-cn");
//            请求体以空行结尾
            writer.println();

//          必须要进行转码，通过返回的文件得知，服务端的编码方式是gbk，一旦错误使用编码将不可逆
            readFromInternet = new BufferedReader(new InputStreamReader(socket.getInputStream(),"gbk"));

            String str;
            while ((str = readFromInternet.readLine()) != null) {
                printer.println(str);
            }
            System.out.println("邓睿");



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (readFromInternet != null) {
                try {
                    readFromInternet.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
